<?php
namespace app\api\controller\product;

// 底层控制器
use think\Controller;

//引入cookie
use think\Cookie;

use think\Request;

class Product extends Controller {
    // 构造函数

    public function __construct() {
        // 手动把父类的方法获取过来
        parent::__construct();

        $this->ProductModel = model( 'Product.Product' );
        $this->TypeModel = model( 'Product.Type' );
        $this->VipModel = model( 'Product.Vip' );
        $this->UserModel = model( 'User.User' );
    }

    // 首页

    public function index() {
        if ( $this->request->isPost() ) {
            // 分类列表
            $typeList = $this->TypeModel->limit( 8 )->select();

            // 推荐
            $recommendList = $this->ProductModel->where( [ 'flag' => 3 ] )->select();

            // 新品
            $newList = $this->ProductModel->where( [ 'flag' => 1 ] )->order( 'id', 'DESC' )->limit( 4 )->select();

            // 热销
            $hotList = $this->ProductModel->where( [ 'flag' => 2 ] )->order( 'id', 'DESC' )->limit( 6 )->select();

            $data = [
                'typeList' => $typeList,
                'recommendList' => $recommendList,
                'newList' => $newList,
                'hotList' => $hotList
            ];

            // 跳转界面
            $this->success( '成功', null, $data );
            exit;
        }
    }

    // 商品列表

    public function type() {
        if ( $this->request->isPost() ) {
            // 获取前端传来的条件
            $typeid = $this->request->param( 'typeid', 0 );
            $keywords = $this->request->param( 'keywords', 0 );
            $flag = $this->request->param( 'flag', 0 );
            $orderby = $this->request->param( 'orderby', 0 );

            // 组合条件
            $where = [];

            // 判断类别id是否为空
            if ( $typeid ) {
                $where[ 'typeid' ] = $typeid;
            }

            // 判断关键字是否为空
            if ( !empty( $keywords ) ) {
                $where[ 'name' ] = [ 'like', "%$keywords%" ];
            }

            // 判断flag是否为空
            if ( $flag > 0 ) {
                $where[ 'flag' ] = $flag;
            }

            // 产品列表
            $productList = $this->ProductModel->where( $where )->order( $orderby, 'DESC' )->select();

            // 分类列表
            $typeList = $this->TypeModel->select();

            $data = [
                'typeList' => $typeList,
                'productList' => $productList,
            ];

            $this->success( '成功', null, $data );
            exit;
        }
    }

    // 商品详情

    public function info() {
        if ( $this->request->isPost() ) {
            // 获取请求数据
            $params = $this->request->param();
            $proid = $this->request->param( 'proid', 0 );
            $userid = $this->request->param( 'userid', 0 );

            if ( $userid != 0 ) {
                // 获取用户信息
                $userinfo = $this->UserModel->find( $userid );

                // 当没有找到用户
                if ( !$userinfo ) {
                    $this->error( '用户不存在，请登录' );
                    exit;
                }
            }

            // 获取商品信息
            $productInfo = $this->ProductModel->find( $proid );

            // 商品详情
            if ( !$productInfo ) {
                $this->error( $this->ProductModel->getError() );
                exit;
            } else {
                // 获取用户vip
                $vip = isset( $userinfo ) ? $userinfo[ 'vip' ] : 0;

                // 查询vip价格
                if ( $vip > 0 ) {
                    $vipPrice = $this->VipModel->where( [ 'proid' => $proid, 'level' => $vip ] )->value( 'price' );
                    $productInfo[ 'vipprice' ] = $vipPrice;
                }

                $this->success( '成功', null, $productInfo );
                exit;
            }
        }
    }
}